﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// LeaveManager 的摘要描述
/// </summary>
public class LeaveManager
{
	public LeaveManager()
	{
		//
		// TODO: 在此加入建構函式的程式碼
		//
	}

    public float GetAvailableLeaveHours(string empID)
    {
        return GetAvailableLeaveHours(empID, DateTime.Now.Year);
    }

    public float GetAvailableLeaveHours(string empID, int yy)
    {
        var db = new DB("Attendance");
        int quota = db.ExecuteScalar<int>(@"SELECT ISNULL(SUM(Amount),0) FROM LeaveQuota WHERE EmpID = @EmpID AND YY = @YY"
                        , new DBParameter("EmpID", empID), new DBParameter("YY", yy));

        float usedHours = db.ExecuteScalar<float>(@"SELECT ISNULL(SUM(Hours),0) FROM LeaveData WHERE EmpID = @EmpID AND Year(LeaveStartDate) = @YY
                            AND Status NOT IN('0','D','N') "
                        , new DBParameter("EmpID", empID), new DBParameter("YY", yy));

        return quota - usedHours;
    }

    public static LeaveManager Current
    {
        get
        {
            if (HttpContext.Current.Items["LeaveManager"] == null)
            {
                HttpContext.Current.Items["LeaveManager"] = new LeaveManager();
            }

            return HttpContext.Current.Items["LeaveManager"] as LeaveManager;
        }
    }
}